package cn.ctbu.xiaopeng.studentscoremanage.service;



import cn.ctbu.xiaopeng.studentscoremanage.anno.Log;
import cn.ctbu.xiaopeng.studentscoremanage.dao.StudentRepository;
import cn.ctbu.xiaopeng.studentscoremanage.pojo.dto.StudentQueryDTO;
import cn.ctbu.xiaopeng.studentscoremanage.pojo.entity.Student;
import cn.ctbu.xiaopeng.studentscoremanage.enums.ResultEnum;
import cn.ctbu.xiaopeng.studentscoremanage.exception.ResultException;
import cn.ctbu.xiaopeng.studentscoremanage.pojo.vo.StudentPageVO;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author xiaopeng
 * @version 1.0
 */
@Service
@RequiredArgsConstructor
public class StudentService {

    private final StudentRepository studentRepository;

    /**
     * 查询所有学生
     *
     * @return
     */
    @Log
    public List<Student> getAllStu() {
        return studentRepository.findAll();
    }

    /**
     * 根据id查询学生
     *
     * @param id
     * @return
     */
    @Log
    public Student findById(Integer id) {
        return studentRepository.findById(id).orElse(null); //orElse(null) 如果查询不到返回null
    }



    /**
     * 根据姓名模糊查询 (like) 需要传递 刘%
     *
     * @param name
     * @return
     */
    @Log
    public List<Student> findByNameLike(String name) {
        return studentRepository.findByNameLike(name);
    }

    /**
     * 根据姓名模糊查询 可直接 刘..
     *
     * @param name
     * @return
     */
    @Log
    public List<Student> findByNameStartWith(String name) {
        return studentRepository.findByNameStartingWith(name);
    }

    /**
     * 根据姓名和密码查询, 用于登录校验
     *
     * @param name
     * @param password
     * @return
     */
    @Log
    public List<Student> findByNameAndPassword(String name, String password) {
        return studentRepository.findByNameAndPassword(name, password);
    }

    /**
     * 添加学生
     *
     * @param student
     */
    @Log
    public Student insert(Student student) {
        return studentRepository.save(student);
    }


    /**
     * 修改学生信息
     * @param student
     */
    @Log
    public Student update(Student student) {
        return studentRepository.save(student);
    }

    /**
     * 删除学生
     * @param id
     */
    public void deleteById(Integer id) {
        studentRepository.deleteById(id);
    }

    /**
     * 根据id批量删除学生
     * @param ids
     */
    @Log
    @Transactional
    public void deleteStudentsByIds(List<Integer> ids) {
        if (ids != null && !ids.isEmpty()) {
            studentRepository.deleteByIds(ids);
        } else {
            throw new ResultException("学生ID列表不能为空");
        }
    }

    /**
     * 登录验证-验证学号和密码是否匹配
     * @param sno
     * @param password
     * @return
     */
    public Student validateSnoAndPsw(String sno, String password) throws Exception{
        Student student = studentRepository.findBySno(sno);
        if (student != null && student.getPassword().equals(password)) {
            //匹配
            return student;
        }else{
            throw new ResultException(ResultEnum.LOGIN_ERROR);
        }
    }

    /**
     * 分页查询
     * @param pageable
     * @return
     */
    @Log
    public Page<Student> getAllWithPage(Pageable pageable){
        return studentRepository.findAll(pageable);
    }


    /**
     * 分页查询
     * @param example
     * @param pageable
     * @return
     */
    @Log
    public Page<Student> getAllWithPage(Example<Student> example, Pageable pageable){
        return studentRepository.findAll(example,pageable);
    }

    /**
     * 根据条件分页查询
     * @param studentQueryDTO
     * @param pageable
     * @return
     */
    @Log
    public Page<StudentPageVO> getStudentPageByConditions(StudentQueryDTO studentQueryDTO, Pageable pageable){
        return studentRepository.getStudentPageByConditions(studentQueryDTO, pageable);
    }

}
